package com.elric.mall.common.api;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * 实现功能：基础controller
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:01
 */
public abstract class BaseController {

    /**
     * 校验参数
     */
    public Boolean validateParams(BindingResult bindingResult, CommonResult commonResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            if (!CollectionUtils.isEmpty(errorList)) {
                commonResult.validateFailed(errorList.get(0).getDefaultMessage());
                return false;
            }
        }
        return true;
    }
}
